package com.ws.testProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 王硕
 * @version jdk21
 * @className FacotryProxy2
 * @description
 * @date 2024/12/2 19:20
 */
public class FacotryProxy2 {
    public static <T> T getProxyInstance(final T target){
        Object o = Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                long begin = System.currentTimeMillis(); //当前系统的毫秒时间
                //2. 执行目标类的方法
                Object obj = method.invoke(target, args);
                //3.计算时间
                System.out.println("耗时:" + (System.currentTimeMillis() - begin));
                return obj;
            }
        });
        return (T) o;
    }
}
